<?php

namespace api\modules\v1\controllers\statistics;

use Yii;
use common\models\base\PatientDiseases;
use common\models\clinic\Clinic;
use api\controllers\OnAuthController;

class ComplexReportController extends OnAuthController
{

    public $modelClass = '';

    /**
     * 科室病人年度在诊周期病人人数汇总表
     *
     * @author wangxiang
     * @return array
     */
    public function actionTreatCycleList()
    {
        return Yii::$app->services->complexReport->getTreatCycleData(['project_id' => $this->projectId]);
    }


    /**
     * 获取复诊构成表下拉选项
     *
     * @author wangxiang
     * @return array
     */
    public function actionReferralSelectInfo()
    {
        // 获取病种
        $diseases = PatientDiseases::find()
            ->where([
                'status' => PatientDiseases::STATUS_ENABLE,
                'pid' => 0
            ])
            ->select(['id value', 'diseases_name title'])
            ->orderBy(['sort' => SORT_ASC])
            ->asArray()
            ->all();

        // 获取诊室
        $clinic = Clinic::find()
            ->where([
                'project_id' => $this->projectId,
                'status' => Clinic::STATUS_ENABLE,
            ])
            ->select(['id value', 'name title'])
            ->orderBy(['sort' => SORT_ASC])
            ->asArray()
            ->all();

        return [
            'diseases' => $diseases,
            'clinic' => $clinic,
        ];
    }

    /**
     * 获取复诊构成表数据
     *
     * @author wangxiang
     * @return array
     */
    public function actionReferralComprise()
    {
        $startRegDate = Yii::$app->request->get('start_reg_date', '');
        $endRegDate = Yii::$app->request->get('end_reg_date', '');
        $startLastRevisitingDate = Yii::$app->request->get('start_last_revisiting_date', '');
        $endLastRevisitingDate = Yii::$app->request->get('end_last_revisiting_date', '');
        $page = (int)Yii::$app->request->get('page', 1);
        $pageSize = (int)Yii::$app->request->get('pageSize', 15);
        $params = [
            'project_id' => $this->projectId,
            'start_reg_time' => $startRegDate ? strtotime($startRegDate) : 0,
            'end_reg_time' => $endRegDate ? strtotime($endRegDate) + 86439 : 0,
            'start_last_revisiting_time' => $startLastRevisitingDate ? strtotime($startLastRevisitingDate) : 0,
            'end_last_revisiting_time' => $endLastRevisitingDate ? strtotime($endLastRevisitingDate) + 86439 : 0,
            'patient_diseases_id' => (int)Yii::$app->request->get('patient_diseases_id', 0),
            'clinic_id' => (int)Yii::$app->request->get('clinic_id', 0),
            'is_visit' => (int)Yii::$app->request->get('is_visit', 0)
        ];
        $params['s_time'] = $params['start_last_revisiting_time'] ? : $params['start_reg_time'];
        $params['e_time'] = $params['end_last_revisiting_time'] ? : $params['end_reg_time'];

        return Yii::$app->services->complexReport->getReferralCompriseData($params, $page, $pageSize);
    }

    /**
     * @api {get} /statistics/complex-report/month-num-list 科室病人月就诊次数人数汇总表
     * @apiGroup 科室病人月就诊次数人数汇总表
     * @apiName complex-report/month-num-list
     *
     * @apiDescription 科室病人月就诊次数人数汇总表
     *
     * @apiParam {String} month_is  月份【2020-06】
     *
     * @apiSuccess {Number} code    状态码，0：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionMonthNumList()
    {
        $monthIs = Yii::$app->request->get('month_is');
        if (!$monthIs) {
            $monthIs = $monthIs ?? date('Y-m', time());
        }
        return Yii::$app->services->monthNumReport->getMonthNum($this->projectId, strtotime($monthIs));
    }

}
